package View.accountsConfiguration;

import Controller.ActionManager;
import Model.xml.protocols.Protocol;
import Model.xml.protocols.StatusNet;
import Model.xml.protocols.Twitter;
import java.awt.Desktop;
import java.awt.Dimension;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * not well designed... needs some thinking
 * @author pieterc
 */
public class addAccount extends JFrame {

    public addAccount() {
        JPanel content = new JPanel();
        this.setContentPane(content);

        String[] protocols = {"twitter", "statusnet"}; // todo: automatically generated
        String accounttype = (String) JOptionPane.showInputDialog(this, "Pick a account", "What microblogging service would you like to catch up",
                JOptionPane.QUESTION_MESSAGE, null, protocols, "statusnet");

        Protocol p = null;

        if (accounttype.equals("twitter")) {
            p = new Twitter();
            if (Desktop.isDesktopSupported()) {
                try {
                    Desktop desktop = Desktop.getDesktop();
                    desktop.browse(new URL((String) p.getInfo()).toURI());
                } catch (URISyntaxException ex) {
                    Logger.getLogger(addAccount.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(addAccount.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            p.setCredentials(JOptionPane.showInputDialog(this, "What is the pincode that you got from twitter?"));

        } else if (accounttype.equals("statusnet")) {
            p = new StatusNet();
            p.setCredentials(JOptionPane.showInputDialog(this, "What is your username?"), JOptionPane.showInputDialog(this, "What is your password?"));

        }
        ActionManager.getRemote().addAccount(p);
        content.add(new JLabel("You may proceed"));
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

        this.setPreferredSize(new Dimension(300, 300));
        this.pack();
    }
}
